<template>
    <div>
        <el-table :data="store.getters['cate/getCate']" border style="width: 100%" row-key="id" default-expand-all
            :tree-props="{ children: 'children' }">
            <el-table-column prop="id" label="分类编号" />
            <el-table-column prop="catename" label="分类名称" />
            <el-table-column prop="img" label="图片">
                <template v-slot="item">
                    <div>
                        <img v-if="item.row.img" :src="item.row.img" alt="" class="img">
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template v-slot="item">
                    <el-tag  effect="light" type="success" size="large"  v-if="item.row.status == 1">启用</el-tag>
                    <el-tag  effect="light" type="warning" size="large" v-else>禁用</el-tag>
                    
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200px">
                <template v-slot="item">
                    <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                    <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script lang="ts" setup>
import { cateedit,catedelete } from '@/request/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineEmits } from "vue"
import { useStore } from 'vuex'

let emit = defineEmits(["edit"])
let store = useStore()
let edit = (id:number) =>{
  emit("edit",id)
}
let del = (id:number) =>{
    ElMessageBox.confirm(
    '删除后不可恢复，是否确认删除？',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
        catedelete({ id }).then(res => {
        if (res.code == 200) {
          getCate()
          ElMessage({
            type: 'success',
            message: '删除成功！',
          })
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除！',
      })
    })


}
let getCate = () =>{
  store.dispatch("cate/getCateListAction")
}
getCate()
</script>
<style lang="less" scoped>
 .img{
    width: 100px;
    height: 100px;
 }
</style>